//
//  YHYReSubDetailViewController.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/28.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYReSubDetailViewController.h"
#import "YHYReSubSpotModel.h"
#import "YHYRESubHeadTableViewCell.h"
#import "SizeForLabel.h"
#import "WJQdetail_listModel.h"

#import "WJQSubNextTableViewCell.h"

//#import "YHYReSubEveryButtonViewController.h"
@interface YHYReSubDetailViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,strong)UITableView *myTableView;
@property (nonatomic,strong)NSMutableDictionary *myDic;
@property (nonatomic,strong)NSMutableDictionary *myDicList;
@property (nonatomic,strong)NSMutableArray *myArray;
@property (nonatomic,strong)NSMutableDictionary *myBigDic;
@property (nonatomic,strong)NSMutableArray *myArraySpot;
@property (nonatomic,assign)NSInteger numberH;
@end

@implementation YHYReSubDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _myDic = [NSMutableDictionary dictionary];
    _myArray = [NSMutableArray array];
    _myDicList = [NSMutableDictionary dictionary];
    _myArraySpot = [NSMutableArray array];
    [self getData];
    [self createTableView];
    

}



- (void)getData{

    NSString *str = nil;
    if (self.everydayModel.spot_id != nil) {
        str = self.everydayModel.spot_id;
    }
    
    NSString *str1 = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@",str];
        //NSLog(@"%@",str);
   
    [WJQNetwork getAFNet:str1 block:^(id result) {

        NSMutableDictionary *dic = [result objectForKey:@"data"];
        
        NSMutableDictionary *dicSpot= [dic objectForKey:@"spot"];
        
        NSMutableArray *spotArr = [NSMutableArray array];
        YHYReSubSpotModel *model = [YHYReSubSpotModel modelWithDicationary:dicSpot];
        model.userName = self.everydayModel.name;
        model.userImageURL = self.everydayModel.avatar_m;
        
        CGSize textHiegt = [SizeForLabel labelRectWithSize:CGSizeMake((375- 17 - 15)*AUTO_HEIGHT, MAXFLOAT) LabelText:model.text Font:[UIFont systemFontOfSize:20]];
        
        model.labelHeigh = textHiegt.height;
        
        [spotArr addObject:model];
        
        [_myDic setObject:spotArr forKey:@"0"];
        
        
        
        
        NSMutableArray *array = [dicSpot objectForKey:@"detail_list"];
        
        NSMutableArray *arrayList = [NSMutableArray array];
        
        for (NSMutableDictionary *dicList in array) {
            
            WJQdetail_listModel *model = [WJQdetail_listModel modelWithDicationary:dicList];
            
            CGSize trip_textHiegt = [SizeForLabel labelRectWithSize:CGSizeMake((375-17 - 15)*AUTO_HEIGHT, MAXFLOAT) LabelText:model.text Font:[UIFont systemFontOfSize:20]];
            
            model.lableHeight = trip_textHiegt.height;
            
            NSInteger photoWidth = model.photo_width;
            
            model.photo_width = SCREEN_WIDTH - 40;
            
            model.photo_height = (NSInteger)((model.photo_height * model.photo_width) / (CGFloat)photoWidth);
            

            [arrayList addObject:model];
        }
        
        
        
        
        
        
        [_myDic setObject:arrayList forKey:@"1"];
        

        
        
        [_myTableView reloadData];
    } errorBlock:^(NSError *error) {
        [self NetworkError:^{
            
            
        }];
        
    }];
    
}


- (void)createTableView{
    
    _myTableView = [UITableView new];
    _myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_myTableView];
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    //去掉cell的线
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_myTableView registerClass:[YHYRESubHeadTableViewCell class] forCellReuseIdentifier:@"YHYRESubHeadTableViewCell"];
    [_myTableView registerClass:[WJQSubNextTableViewCell class] forCellReuseIdentifier:@"WJQSubNextTableViewCell"];
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _myDic.allKeys.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    NSString *key = [NSString stringWithFormat:@"%ld", section];
    
    return [[_myDic objectForKey:key] count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *key = [NSString stringWithFormat:@"%ld", indexPath.section];
    
    if (0 == indexPath.section) {
        
        YHYReSubSpotModel *mod = [_myDic[key] lastObject];
        return 140 + mod.labelHeigh;
        
    } else {
        
        NSMutableArray *arr = _myDic[key];
        WJQdetail_listModel *mod = [arr objectAtIndex:indexPath.row];
        return mod.photo_height + mod.lableHeight + 30;
    }
   
 }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
    
    static NSString *headCellStr = @"YHYRESubHeadTableViewCell";
    static NSString *nextCellStr = @"WJQSubNextTableViewCell";
    
    NSString *key = [NSString stringWithFormat:@"%ld", indexPath.section];
    
    if (0 == indexPath.section) {
        
        YHYRESubHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:headCellStr];

        cell.myModel = [_myDic[key] lastObject];
 cell.selectionStyle= UITableViewCellSelectionStyleNone;

        return cell;
    } else {
        
        WJQSubNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nextCellStr];
                 
        cell.listMod = [_myDic[key] objectAtIndex:indexPath.row];
 cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
        WJQdetail_listModel *model = [WJQdetail_listModel new];
        model = [_myDic[key] objectAtIndex:indexPath.row];
       
        if (model.text.length > 0) {
            [cell setisHidden:NO];
        }
        else{
            [cell setisHidden:YES];
        }
        
        
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
